<!--
 * @Author: windys
 * @Description: 微信登录获取code
 * @Date: 2022-02-11 17:08:51
-->

<template>
  <div class="code_page">
    {{ data }}
    <van-loading style="margin-top: 50%" size="100px" vertical color="#4b7dee"
      >微信登录中...</van-loading
    >
  </div>
</template>


<script>
import { defineComponent, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { wxLogin } from "@/api/login";
import storage from "@/utils/storage.js";
import { Notify, Toast } from "vant";
export default defineComponent({
  setup() {
    const route = useRoute();
    const router = useRouter();
    const url = window.location.search.substring(1).split("&");
    var obj = {};
    url.map((item) => {
      obj[item.split("=")[0]] = item.split("=")[1];
    });
    const code = ref("");
    const data = ref("11");
    // 微信提供code
    if (obj.code) {
      code.value = obj.code;
      wxLogin({
        code: code.value,
        inviteCode: window.localStorage.getItem("JSfrom") || "",
      }).then((res) => {
        data.value = res;
        // 登录成功
        if (typeof res !== "number" && res.user) {
          storage.setStorage("v3_userinfo_mobile", JSON.stringify(res.user));
          storage.setStorage("token", res.token);
          storage.setStorage("pwd", JSON.stringify({}));
          router.push("/my");
        } else if (res === 8001) {
          //首次使用 需要绑定微信
          Notify({
            type: "warning",
            message: "首次使用微信登录需要先绑定手机号",
          });
          router.push({
            path: "/wxBindLogin",
            query: {
              unionId: res.unionId,
              nick: res.nick,
              headUrl: res.headUrl,
            },
          });
        } else {
          window.localStorage.clear();
          router.push("/login");
        }
      });
    }
    return {
      code,
      data,
    };
  },
});
</script>



<style lang="scss" scoped>
.code_page {
  text-align: center;
  font-size: 12px;
}
</style>